package com.yhy.produce.controller;

import cn.hutool.json.JSONUtil;
import com.yhy.common.core.exception.BizException;
import com.yhy.common.core.result.Result;
import com.yhy.common.core.utils.ContextUtil;
import com.yhy.produce.feign.RemoteApiFeign;
import com.yhy.produce.feign.RemoteConsumeFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "test测试")
@Slf4j
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
public class TestController {

    final RemoteApiFeign remoteApiFeign;
    final RemoteConsumeFeign remoteConsumeFeign;

    @ApiOperation("测试接口0")
    @GetMapping("/0")
    public Result<?> feign(@RequestParam("param") String param) {
        log.info("测试日志");
        Map<String, Object> map = new HashMap<>();
        map.put("id", 123);
        Result<?> test = remoteApiFeign.test(map);
        Result<?> result = remoteApiFeign.test1(param);
        Result<?> test1 = remoteConsumeFeign.test(123L);
        return Result.ok(ContextUtil.getUser());
    }

    @ApiOperation("测试接口1")
    @GetMapping("/1")
    public Result<?> test1(@ApiParam("参数id") @RequestParam("id") Long id) {
        return Result.ok(id);
    }

    @ApiOperation("测试接口2")
    @PostMapping("/2")
    public Result<?> test2(@RequestBody Map<String, Object> paramMap) {
        return Result.ok(paramMap);
    }

    @ApiOperation("测试接口3-异常")
    @GetMapping("/3")
    public Result<?> test3() {
        throw new BizException("自定义异常");
    }

    @ApiOperation("测试接口4")
    @GetMapping("/4/{id}")
    public Result<?> test4(@PathVariable("id") String id) {
        return Result.ok("接口返回: " + id);
    }

    @ApiOperation("测试接口5")
    @PatchMapping("/5/{id}")
    public Result<?> test5(@PathVariable("id") String id,
                           @RequestBody Map<String, Object> paramMap) {
        log.info("PathVariable: {}", id);
        log.info("RequestBody: {}", JSONUtil.toJsonStr(paramMap));
        return Result.ok();
    }

}